import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StarterAssignment1And2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		MyFileReader fileReader = new MyFileReader("2002-05-01.xml");
		
		Pattern beginClassPattern = Pattern.compile(".*<Model:Class .*", Pattern.DOTALL);
		Pattern classNamePattern = Pattern.compile(".*name = '([^']*)'.*", Pattern.DOTALL);
		Pattern endClassPattern = Pattern.compile(".*</Model:Class>.*", Pattern.DOTALL);
		Pattern classAttributePattern = Pattern.compile(".*<Model:Attribute .*", Pattern.DOTALL);
		boolean classBegan = false;
		boolean attributeBegan = false;
		String className = null;
		
		BufferedReader reader = new BufferedReader(new StringReader(fileReader.read().toString()));
		HashMap<String, Integer> classAttributeCount = new HashMap<String, Integer>();
		String line = null;
		Matcher m ;
		try {
			while ((line = reader.readLine()) != null) {
				if (classBegan){
					m = classAttributePattern.matcher(line);
					if (m.matches()){
						int allreadyCounted = classAttributeCount.get(className);
						classAttributeCount.put(className, allreadyCounted+1);
						System.out.println(line);
						attributeBegan = true;
					}
					
					if (!attributeBegan){
						m = classNamePattern.matcher(line);
						if (m.matches()){
							System.out.println(line);
							className = m.group(1);
							System.out.println(className);
							classAttributeCount.put(className, 0);
						}
					}
					
					m = endClassPattern.matcher(line);
					if (m.matches()){
						classBegan = false;
						System.out.println(line);
						if (className == null)
							System.err.println("NOT FOUND?!?");
						className = null;
						attributeBegan = false;
					}
				}
				else{					
					m = beginClassPattern.matcher(line);
					if (m.matches()){
						classBegan = true;
						System.out.println(line);
					}
				}
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		printMost(classAttributeCount);
		
	}
	
	private static void printMost(HashMap<String, Integer> classAttributeCount){
		System.out.println("class count = "+classAttributeCount.size());
		
		Entry<String, Integer> maxEntry = null;
		for (Entry<String, Integer> entry : classAttributeCount.entrySet()){
			if (maxEntry == null || entry.getValue() > maxEntry.getValue())
				maxEntry = entry;
		}
		
		System.out.println(maxEntry.getKey()+" : "+maxEntry.getValue());
	}

}
